package com.haixiaoke.saas.enterprise.apartment.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.apartment.domain.CalendarPrice;
import com.haixiaoke.saas.apartment.domain.HousingAllocation;
import com.haixiaoke.saas.apartment.service.ICalendarPriceService;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 价格日历Controller
 *
 * @author panda
 * @date 2024-03-29
 */
@RestController
@RequestMapping("/enterprise/apartment/calendarPrice")
public class EnterpriseCalendarPriceController extends BaseController {
    @Autowired
    private ICalendarPriceService calendarPriceService;

    /**
     * 查询价格日历列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:calendarPrice:list')")
    @GetMapping("/list")
    public AjaxResult list(CalendarPrice calendarPrice) {
        List<CalendarPrice> list = calendarPriceService.selectCalendarPriceList(calendarPrice);
        return success(list);
    }

    /**
     * 查询房间下指定时间范围的价格日历集合
     */
    @PreAuthorize("@ss.hasPermi('enterprise:calendarPrice:list')")
    @GetMapping("/listByAllocation")
    @TenantScope
    public TableDataInfo listByAllocation(HousingAllocation allocation) {
        if (ObjectUtil.isEmpty(allocation.getStartTime()) || ObjectUtil.isEmpty(allocation.getEndTime())) {
            throw new ServiceException("请选择时间!");
        }
        startPage();
        allocation.setTenantId(TenantContext.getTenantId());
        List<HousingAllocation> list = calendarPriceService.selectCalendarPriceListByAllocation(allocation);
        return getDataTable(list);
    }

    /**
     * 导出价格日历列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:calendarPrice:export')")
    @Log(title = "价格日历", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CalendarPrice calendarPrice) {
        List<CalendarPrice> list = calendarPriceService.selectCalendarPriceList(calendarPrice);
        ExcelUtil<CalendarPrice> util = new ExcelUtil<CalendarPrice>(CalendarPrice.class);
        util.exportExcel(response, list, "价格日历数据");
    }

    /**
     * 获取价格日历详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:calendarPrice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(calendarPriceService.selectCalendarPriceById(id));
    }

    /**
     * 新增价格日历
     */
    @PreAuthorize("@ss.hasPermi('enterprise:calendarPrice:add')")
    @Log(title = "价格日历", businessType = BusinessType.INSERT)
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody CalendarPrice calendarPrice) {
        if (calendarPrice.getAllocationId() == null || StrUtil.isBlank(calendarPrice.getHouseId())) {
            return warn("请选择房屋");
        }
        if (calendarPrice.getCalendarPrice() == null) {
            return warn("请输入改后价格");
        }
        if (BigDecimal.ZERO.compareTo(calendarPrice.getCalendarPrice()) >= 0) {
            return warn("日历价格需要大于0");
        }
        calendarPrice.setCreateBy(getUsername());
        calendarPrice.setTenantId(TenantContext.getTenantId());
        return toAjax(calendarPriceService.insertCalendarPrice(calendarPrice));
    }

    /**
     * 修改价格日历
     */
    @PreAuthorize("@ss.hasPermi('enterprise:calendarPrice:edit')")
    @Log(title = "价格日历", businessType = BusinessType.UPDATE)
    @PutMapping
    @TenantScope
    public AjaxResult edit(@RequestBody CalendarPrice calendarPrice) {
        if (calendarPrice.getAllocationId() == null || StrUtil.isBlank(calendarPrice.getHouseId())) {
            return warn("请选择房屋");
        }
        if (calendarPrice.getCalendarPrice() == null) {
            return warn("请输入改后价格");
        }
        if (BigDecimal.ZERO.compareTo(calendarPrice.getCalendarPrice()) >= 0) {
            return warn("日历价格需要大于0");
        }
        calendarPrice.setUpdateBy(getUsername());
        calendarPrice.setTenantId(TenantContext.getTenantId());
        return toAjax(calendarPriceService.updateCalendarPrice(calendarPrice));
    }

    /**
     * 修改价格日历
     */
    @PreAuthorize("@ss.hasPermi('enterprise:calendarPrice:edit')")
    @Log(title = "价格日历", businessType = BusinessType.UPDATE)
    @PutMapping("/editBatch")
    @TenantScope
    public AjaxResult editBatch(@RequestBody CalendarPrice calendarPrice) {
        if (calendarPrice.getHouseIds() == null || calendarPrice.getHouseIds().length == 0) {
            return warn("请选择房屋");
        }
        if (ObjectUtil.isEmpty(calendarPrice.getStartTime()) || ObjectUtil.isEmpty(calendarPrice.getEndTime())) {
            return warn("请选择时间!");
        }
        if (calendarPrice.getStartTime().after(calendarPrice.getEndTime())) {
            return error("时间数据错误!");
        }
        if (calendarPrice.getCalendarPrice() == null) {
            return warn("请输入改后价格");
        }
        if (BigDecimal.ZERO.compareTo(calendarPrice.getCalendarPrice()) >= 0) {
            return warn("日历价格需要大于0");
        }
        calendarPrice.setUpdateBy(getUsername());
        calendarPrice.setTenantId(TenantContext.getTenantId());
        return toAjax(calendarPriceService.updateBatchCalendarPrice(calendarPrice));
    }

    /**
     * 删除价格日历
     */
    @PreAuthorize("@ss.hasPermi('enterprise:calendarPrice:remove')")
    @Log(title = "价格日历", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(calendarPriceService.deleteCalendarPriceByIds(ids));
    }
}
